//
//  LocationTestViewController.m
//  LocationTest
//
//  Created by MAC08 on 07/05/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "LocationTestViewController.h"

@implementation LocationTestViewController



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark - Other Function 
-(void) SendUserInfoToServer
{
	

	NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
	[dic setObject:[txtfirst.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] forKey:@"name"];
	[dic setObject:[txtSecond.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] forKey:@"email"];
	
	BOOL isReadyToCallWS = TRUE;
	if(IsUserLocationShare)
	{
		//Check Eithor GPS Setting On for User Location or not
		if([AppDel CheckLocationService])
		{
			[dic setObject:[NSString stringWithFormat:@"%f,%f",AppDel.newlocation.coordinate.latitude,AppDel.newlocation.coordinate.longitude] forKey:@"location"];
	    }
		else {
			isReadyToCallWS = FALSE;
		}

		
	}
	
	
	// Call WS to send Data to Server
	int status=0;
	if(isReadyToCallWS)
	{	status = [AppDel.objWS sendDataToServer:dic];
	    if(status == 1)
		{
			//UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Send Data Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//			[alert show];
//			[alert release];
			errorlblfieldsorjobstatus.hidden = FALSE;
			errorlblfieldsorjobstatus.text = @"Send Data Successfully";
			[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(HideErrorMessage:) userInfo:@"1" repeats:NO];
			txtfirst.text = @"";
			txtSecond.text = @"";
		}
		else 
		{
//			UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"Send Data fail" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//			[alert show];
//			[alert release];
			errorlblfieldsorjobstatus.text = @"Send Data fail";
			errorlblfieldsorjobstatus.hidden = FALSE;
			[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(HideErrorMessage:) userInfo:@"0" repeats:NO];

		}
	}
	[dic release];
	
}

#pragma mark -
#pragma mark - Button Event
-(IBAction) SendBtnClick:(id)sender
{

	NSString *txtfirststr = [txtfirst.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	NSString *txtSecondstr = [txtSecond.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
	NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
	NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex]; 
	
    if([txtfirststr length] ==0 || [txtSecondstr length] == 0)
	{
//		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Please, Enter Both Values" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//		[alert show];
//		[alert release];
		errorlblfieldsorjobstatus.text = @"Please, Enter Both Values";
		errorlblfieldsorjobstatus.hidden = FALSE;
		[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(HideErrorMessage:) userInfo:@"2" repeats:NO];
		
	} 
	else if(![emailTest evaluateWithObject:txtSecondstr])
	{
//		UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Please, Enter valid email address" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//		[alert show];
//		[alert release];
		errorlblemail.text = @"Please, Enter valid email address";
		errorlblemail.hidden = FALSE;
		[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(HideErrorMessage:) userInfo:@"3" repeats:NO];
	}
	else 
	{
		if(![[NSUserDefaults standardUserDefaults] objectForKey:@"ZZZ_LOCATION_TEST_FIRST_TIME_ZZZ"])
		{
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Do you want to share your location." delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];
			[alert show];
			[alert release];
		}
		else 
		{
			/*
			 If Userdefault value is 1 than user allow location sharning otherwise not allow.
			 */
			IsUserLocationShare = [[[NSUserDefaults standardUserDefaults] objectForKey:@"ZZZ_LOCATION_TEST_FIRST_TIME_ZZZ"] intValue]==1?TRUE:FALSE;
			[self SendUserInfoToServer];	
		}
	}
}

#pragma mark -
#pragma mark - UIAlertView Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

	if(buttonIndex == 1)
	{
	  	
		IsUserLocationShare = TRUE;
		[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"ZZZ_LOCATION_TEST_FIRST_TIME_ZZZ"];
	}
	else 
	{
		IsUserLocationShare = FALSE;
		[[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"ZZZ_LOCATION_TEST_FIRST_TIME_ZZZ"];

	}
	[[NSUserDefaults standardUserDefaults] synchronize];
}

-(void) HideErrorMessage:(id)sender
{
 	int flag = [(NSString *)[sender userInfo] intValue];
	switch (flag) {
		case 0:
		case 1:
		case 2:
			errorlblfieldsorjobstatus.hidden = TRUE;
			break;
		case 3:
			errorlblemail.hidden = TRUE;
			break;
		default:
			break;
	}
}

@end
